package com.dnw.shyfunny;

import android.Manifest;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.dnw.adapter.LocalShowAdapter;
import com.dnw.base.BaseActivity;
import com.dnw.base.BasePresenter;
import com.dnw.listener.PermissionListener;
import com.dnw.util.FileUtils;
import com.dnw.util.ListUtils;
import com.dnw.util.UIUtils;
import com.google.common.collect.Lists;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import flyn.Eyes;

/**
 * Created by yangyu on 2017/11/16.
 * 关于页面
 */

public class LocalActivity extends BaseActivity {

    //    @Bind(R.id.tv_back)
    //    TextView tvBack;
    //
    //    @Bind(R.id.tv_version)
    //    TextView tvVersion;
    //
    @Bind(R.id.tv_author)
    TextView tvAuthor;

    //    @Bind(R.id.id_recyclerview)
    RecyclerView id_recyclerview;
    private RecyclerView.LayoutManager mLayoutManager;

    private LocalShowAdapter mLocalShowAdapter;
    List<String> list = Lists.newArrayList();

    @Override
    public void initView() {
        Eyes.setStatusBarColor(this, UIUtils.getColor(R.color.a));
    }

    @Override
    public void initData() {
        tvAuthor.setText(getResources().getString(R.string.side_column_local));
        requestRuntimePermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionListener() {
            @Override
            public void onGranted() {
                list = getPictures(FileUtils.getDir("shyFunny"));
                if (!ListUtils.isEmpty(list)) {
                    id_recyclerview = (RecyclerView) findViewById(R.id.id_recyclerview);
                    id_recyclerview.setLayoutManager(new GridLayoutManager(LocalActivity.this, 3));
                    id_recyclerview.setAdapter(new LocalShowAdapter(LocalActivity.this, list));
                }
            }

            @Override
            public void onDenied(List<String> deniedPermissions) {
                UIUtils.showToast(getString(R.string.read_storage_permission_deny));
            }
        });


    }


    public List<String> getPictures(final String strPath) {
        List<String> list = new ArrayList<String>();
        File file = new File(strPath);
        File[] allfiles = file.listFiles();
        if (allfiles == null) {
            return null;
        }
        for (int k = 0; k < allfiles.length; k++) {
            final File fi = allfiles[k];
            if (fi.isFile()) {
                int idx = fi.getPath().lastIndexOf(".");
                if (idx <= 0) {
                    continue;
                }
                String suffix = fi.getPath().substring(idx);
                if (suffix.toLowerCase().equals(".jpg") ||
                        suffix.toLowerCase().equals(".jpeg") ||
                        suffix.toLowerCase().equals(".bmp") ||
                        suffix.toLowerCase().equals(".png") ||
                        suffix.toLowerCase().equals(".gif")) {
                    list.add(fi.getPath());
                }
            }
        }
        return list;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_local;
    }

    @OnClick(R.id.tv_back)
    public void onViewClicked() {
        finish();
    }
}
